package com.psdi.element;

import com.psdi.manager.ElementManager;
import com.psdi.manager.EnemySystem;
import com.psdi.manager.GameLoader;
import com.psdi.manager.GameManager;
import com.psdi.physics.Collider;
import com.psdi.physics.CollisionSystem;
import com.psdi.window.GameJFrame;

import javax.swing.*;
import java.util.Collections;
import java.util.Random;
import java.util.Set;

/**
 * 敌人类
 * 用于处理游戏中的敌人元素
 */
public class Enemy extends ElementObject implements Collider {
    private float hp;
    private float atk = 1;
    Vector2D direction = Vector2D.LEFT; //敌人移动方向
    private float speed = 0.5f; //敌人移动速度
    private Player targetPlayer;
    private final Vector2D[] directions = {
            Vector2D.LEFT, Vector2D.RIGHT, Vector2D.UP, Vector2D.DOWN
    }; // 敌人可能的移动方向
    private final Random random = new Random();

    public Enemy(int x, int y, int width, int height, ImageIcon image) {
        super(x, y, width, height, image);
        this.tag = Tag.ENEMY;
        CollisionSystem.INSTANCE.addElement(this);
    }

    public Enemy() {

    }

    @Override
    public Size colliderSize() {
        return transform.size;
    }

    @Override
    public Position colliderPosition() {
        return transform.position;
    }

    @Override
    public Tag colliderTag() {
        return tag;
    }

    @Override
    public Set<Tag> colliderFilterTag() {
        return Collections.singleton(Tag.MAP);
    }

    @Override
    public void onCollisionStay(ElementObject other) {
        if (other instanceof Bullet || other instanceof Knife ||other instanceof KnifeAirflow) {
            setAlive(checkconditon()); // 被子弹击中时，敌人死亡
        } else if (other instanceof Player) {
            other.setAlive(((Player) other).checkCondition()); // 敌人碰到玩家时，玩家检测死亡
            ((Player) other).setHp(((Player) other).getHp() - atk);
            other.transform.position.add(direction.scalarMultiply(40));
        }
//        else if (other instanceof MapObject) {
//            // 敌人与地图对象碰撞时，尝试向其他三个方向移动
//            // 先解除当前位置的碰撞
//            transform.position.add(direction.scalarMultiply(-2));
//            // 随机选择一个方向
//            direction = directions[random.nextInt(directions.length)];
//        }
    }
    private boolean checkconditon(){
        if(hp <= 0){
            GameManager.gameManager.killNum++;
            return false;
        }
        else return true;
    }
    @Override
    public void update() {
        //获取Player
        if(!ElementManager.getINSTANCE().getGameElements().get(Tag.PLAYER).isEmpty()) {
            targetPlayer = (Player)ElementManager.getINSTANCE().getGameElements().get(Tag.PLAYER).get(0);
        }
        // 敌人移动逻辑
        // 这里可以添加敌人的行为逻辑，例如移动、攻击等
        if (targetPlayer != null && targetPlayer.isAlive()) {
            trackPlayer();
        }
        updateImage();
    }

    private void trackPlayer() {
        Vector2D directionToPlayer = Vector2D.displacement(transform.position, targetPlayer.transform.position).normalize();

        transform.position.add(directionToPlayer.scalarMultiply(speed));

        updateDirection(directionToPlayer);

        checkBounds();
    }


    private void updateDirection(Vector2D directionToPlayer) { //更新朝向
        if (Math.abs(directionToPlayer.x) > Math.abs(directionToPlayer.y)) {
            //水平移动为主
            direction = directionToPlayer.x > 0 ? Vector2D.RIGHT : Vector2D.LEFT;
        } else {
            //垂直移动为主
            direction = directionToPlayer.y > 0 ? Vector2D.DOWN : Vector2D.UP;
        }
    }

    private void checkBounds() { //检查边界
        if (transform.position.x < 0) {
            transform.position.x = 0;
        } else if (transform.position.x + transform.size.width > GameJFrame.GAME_WIDTH) {
            transform.position.x = GameJFrame.GAME_WIDTH - transform.size.width;
        }
        if (transform.position.y < 0) {
            transform.position.y = 0;
        } else if (transform.position.y + transform.size.height > GameJFrame.GAME_HEIGHT) {
            transform.position.y = GameJFrame.GAME_HEIGHT - transform.size.height;
        }
    }

    @Override
    protected void updateImage() {
        setImage(GameLoader.enemyIconMap.get(direction));
    }

    @Override
    public void onDestroy() {
        GameLoader.LoadBoom(this.transform.position,this.transform.size);
        EnemySystem.getINSTANCE().curruntNum--;
    }

    public ElementObject CreateEnemy(Position pos,float Hp,float Speed) {
        this.image = GameLoader.enemyIconMap.get(direction);
        this.transform = new Transform(pos,new Rotation(),new Size(30, 30));
        this.tag=Tag.ENEMY;
        CollisionSystem.INSTANCE.addElement(this);
        hp = Hp;
        speed =Speed;
        return this;
    }
    public float getHp() {
        return hp;
    }

    public void setHp(float hp) {
        this.hp = hp;
    }

}